$(function() {
    var webUrl= window.location.protocol+"://"+window.location.host;
    var currEl;
    var picJcrop;
    var multiPicIndex=0;
    var mediaIndex=0;

    //获取当前编辑对象
    $('body.edit .demo').on("click",".editPic",function(e) {
        e.preventDefault();
        currEl = $(this).parent().parent().find('.view');
    });

    //加载图片
    $('#editPic').on('show.bs.modal', function () {
        //手机页面中是否显示本模块
        if(currEl.hasClass('hidden-xs')){
            $("#picMobileHide").attr("checked",true);
        }else{
            $("#picMobileHide").attr("checked",false);
        }
        var imgsrc = currEl.find("img").attr("src");
        $('#editPic').find('.image-wrapper').children('img').attr("src", imgsrc);
        $("#picLink").val(currEl.children("a").attr("href"));
    });

    $('#editMultiPic').on('show.bs.modal', function () {
        //手机页面中是否显示本模块
        if(currEl.hasClass('hidden-xs')){
            $("#multiPicMobileHide").attr("checked",true);
        }else{
            $("#multiPicMobileHide").attr("checked",false);
        }
        //设定显示比例
        var imgwidth = currEl.width();
        var imgheight = currEl.height();
        var scale = (imgwidth/imgheight).toFixed(1);
        $("#multiPicScale").val(adaptScaleRatio(scale));
        var imgs = currEl.find(".carousel-inner").find("img");
        var imgsrcs = [];
        for(var i=0; i<imgs.length; i++) {
            var img = imgs[i];
            imgsrcs.push($(img).attr("src"));
        }
        var dom = $('#editMultiPic').find("#addMultiPic");
        dom.parent().prevAll(".form-group").remove();
        addPic(dom, imgsrcs);
    });

    $('#editMultiPic').on('hidden.bs.modal', function () {
        multiPicIndex = 0;
    });

    $('#editMedia').on('show.bs.modal', function () {
        $("#mediaTitle").val(currEl.find(".titleBox .title").text());
        $("#mediaSubTitle").val(currEl.find(".titleBox .subTitle").text());
        if(currEl.find(".titleBox").hasClass('hidden')){
            $("#mediaTitleHide").attr("checked",true);
        }else{
            $("#mediaTitleHide").attr("checked",false);
        }
        if(currEl.hasClass('hidden-xs')){
            $("#mediaMobileHide").attr("checked",true);
        }else{
            $("#mediaMobileHide").attr("checked",false);
        }
        var items = currEl.find(".the-item");
        var medias = [];
        for(var i=0; i<items.length; i++) {
            var item = items[i];
            medias.push({img:$(item).find(".media-wrapper img").attr("src"),
                         name:$(item).find(".memo-wrapper .memoName").text(),
                         desc:$(item).find(".memo-wrapper .memoDesc").text(),
                         video:$(item).find(".media-wrapper input").val()});
        }
        var dom = $('#editMedia').find("#addMedia");
        dom.parent().prevAll(".the-item").remove();
        addMedia(dom, medias);
    });

    $('#editMedia').on('hidden.bs.modal', function () {
        mediaIndex = 0;
    });

    //加载视频
    $(document).on("click",".playicon",function(e) {
        e.preventDefault();
        var videosrc = $(this).next("input").val();
        if(videosrc!='') {
            $("#videoModal").find(".videoPlayer iframe").attr("src", videosrc);
            $("#videoModal").modal('show');
        }
    });

    $('#videoModal').on('hidden.bs.modal', function () {
        $('#videoModal').find(".videoPlayer iframe").attr("src","");
    });

    //加载相册
    $('#editAlbum').on('show.bs.modal', function () {
        $("#albumTitle").val(currEl.find(".titleBox .title").text());
        $("#albumSubTitle").val(currEl.find(".titleBox .subTitle").text());
        if(currEl.find(".titleBox").hasClass('hidden')){
            $("#albumTitleHide").attr("checked",true);
        }else{
            $("#albumTitleHide").attr("checked",false);
        }
        if(currEl.hasClass('hidden-xs')){
            $("#albumMobileHide").attr("checked",true);
        }else{
            $("#albumMobileHide").attr("checked",false);
        }
        var albumId = currEl.find(".albumList").attr("data-id");
        var showCount = currEl.find(".albumList").attr("data-count");
        var showTitle = currEl.find(".albumList").attr("data-title");
        var albumListGroup = $('#editAlbum').find("#albumContent form .form-group")[0];
        var albumList = $(albumListGroup).children("div .col-sm-10");
        albumList.empty();
        $.ajax({
            url:"/action/ablum.asp?act=type_list",
            type:"post",
            dataType:"json",
            success:function (data) {
                if(data.status==0) {
                    if(data.typelist && data.count>0) {
                        for(var i=0; i<data.typelist.length; i++) {
                            var album = data.typelist[i];
                            albumList.append("<label class=\"radio-inline\">\n" +
                                                "<input type=\"radio\" name=\"albumListRadio\" value=\""+album.typeid+"\" > "+album.typename+"\n" +
                                             "</label>");
                        }
                        $("#albumContent input[name='albumListRadio'][value='" + (albumId?albumId:0) + "']").prop("checked", "checked");
                        $("#albumDisplayCount").val(showCount?showCount:"");
                        $("#albumContent input[name='imgShowTitle'][value='" + (showTitle?showTitle:1) + "']").prop("checked", "checked");
                    }
                }else {
                    alert("加载相册列表失败！");
                }
            }
        })
    });

    //加载联系我们
    $('#editContact').on('show.bs.modal', function () {
        $("#contactTitle").val(currEl.find(".titleBox .title").text());
        $("#contactSubTitle").val(currEl.find(".titleBox .subTitle").text());
        if(currEl.find(".titleBox").hasClass('hidden')){
            $("#contactTitleHide").attr("checked",true);
        }else{
            $("#contactTitleHide").attr("checked",false);
        }
        if(currEl.hasClass('hidden-xs')){
            $("#contactMobileHide").attr("checked",true);
        }else{
            $("#contactMobileHide").attr("checked",false);
        }
        var linkman = currEl.find(".contactCnt").attr("data-linkman");
        var mobile = currEl.find(".contactCnt").attr("data-mobile");
        var abstract = currEl.find(".contactCnt").attr("data-abstract");
        var address = currEl.find(".contactCnt").attr("data-address");
        $("#contactshowLinkman").prop("checked", !linkman || linkman=="false"?false:true);
        $("#contactShowMobile").prop("checked", !mobile || mobile=="false"?false:true);
        $("#contactShowAbstract").prop("checked", !abstract || abstract=="false"?false:true);
        $("#contactShowAddress").prop("checked", !address || address=="false"?false:true);
    });

    //添加图片
    $("#addMultiPic").click(function(e) {
        e.preventDefault();
        addPic(this);
    });

    $("#addMedia").click(function(e) {
        e.preventDefault();
        addMedia(this);
    });

    //图片上传
    $(".btn-uploadpic").fileupload({
        url: '/action/upload/',
        dataType: 'json',
        add: function (e, data) {
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { fileinpname: 'pic', time: Date() }
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.files[0].name;
                filename = filename.substring(0,filename.length-4);
                $(this).parent().parent().prev('.image-wrapper').children('img').attr("id","pic-"+filename).attr("src",data.result.relatPath).attr("height","200").attr("width","300");
            }
        }
    });

    //图片裁剪
    $('#currCutPic').Jcrop({
        allowSelect: true,
        baseClass: 'jcrop',
        boxWidth: 770,
    }, function() {
        picJcrop = this;
    });

    $(document).on("click",".btn-cutpic",function(e) {
        e.preventDefault();
        var imgsrc = $(this).parent().prev('.image-wrapper').children('img').attr("src");
        picJcrop.setImage(imgsrc);
        $("#cutPicModal").modal('show');
    });

    //删除图片
    $(document).on("click",".btn-delpic",function(e) {
        e.preventDefault();
        if(confirm("确认要删除此组元素吗？")) {
            $(this).parents('.the-item').remove();
        }
    });

    //图片保存
    $("#savePic").click(function(e) {
        e.preventDefault();
        var imgs = $(this).parent().parent().find(".pcontent .image-wrapper img");
        if(imgs && imgs.length==1) {
            currEl.find("img").attr("src",$(imgs[0]).attr("src"));
        }
        if($("#picMobileHide").is(":checked")){
            currEl.addClass("hidden-xs");
        }else{
            currEl.removeClass("hidden-xs");
        }
        currEl.find("a").attr("href",$("#picLink").val());
    });
    $("#saveMultiPic").click(function(e) {
        e.preventDefault();
        var imgs = $(this).parent().parent().find(".pcontent .image-wrapper img");
        var urls = $(this).parent().parent().find(".pcontent .link-url");
        var scale = $("#multiPicScale").val();
        if(imgs) {
            var imgwidth = currEl.width();
            var imgheight = imgwidth / scale;
            var id = currEl.children("div").attr("id");
            var imgIndicator = currEl.find(".carousel-indicators");
            var imgContainer = currEl.find(".carousel-inner");
            imgIndicator.empty();
            imgContainer.empty();
            for(var i=0; i<imgs.length; i++) {
                var img = imgs[i];
                var url = urls[i];
                imgIndicator.append("<li "+(i==0 ? "class=\"active\"":"")+" data-slide-to=\""+i+"\" data-target=\""+id+"\"></li>");
                imgContainer.append("<div class=\"item "+(i==0 ? "active":"")+"\"><a href='"+($(url).val()!=''?$(url).val():"#")+"'><img src=\""+$(img).attr("src")+"\" style='height: "+1/scale+"%'></a></div>");
            }
        }
        if($("#multiPicMobileHide").is(":checked")){
            currEl.addClass("hidden-xs");
        }else{
            currEl.removeClass("hidden-xs");
        }
    });
    $("#saveMedia").click(function(e) {
        e.preventDefault();
        currEl.find(".titleBox .title").text($("#mediaTitle").val());
        currEl.find(".titleBox .subTitle").text($("#mediaSubTitle").val());
        var items = $(this).parent().parent().find(".pcontent .the-item");
        if(items) {
            var mediaList = currEl.find(".mediaList");
            mediaList.empty();
            var itemel = "<div class=\"onerow\">\n";
            for(var i=0; i<items.length; i++) {
                var item = items[i];
                var data = {};
                data.img = $(item).find(".image-wrapper img").attr("src");
                data.name = $(item).find("input[name='name']").val();
                data.desc = $(item).find("textarea[name='desc']").val();
                data.video = $(item).find("input[name='video']").val();
                if(/^<iframe/.test(data.video)) {
                    var src = data.video.match(/src=("|')[^"']*("|')/)[0];
                    if(/^src=/.test(src)) {
                        data.video = src.substring(5,src.length-1);
                    }else {
                        data.video = "";
                    }
                }
                itemel += '<div class="col-md-6 the-item">\n' +
                    '          <div class="media-wrapper">\n' +
                    '              <img src="'+data.img+'" alt="" width="100%" height="100%">\n' +
                    '              <div title="播放" class="'+(data.video!="" ? "video playicon":"hidden")+'">' +
                    '                  <div class="tri"></div>' +
                    '              </div>\n' +
                    '              <input type="hidden" value="'+data.video+'">\n' +
                    '          </div>\n' +
                    '          <div class="memo-wrapper">\n' +
                    '              <h4 class="memoName">'+data.name+'</h4>\n' +
                    '              <div class="memoDesc">'+data.desc+'</div>\n' +
                    '          </div>\n' +
                    '      </div>\n';
                if(i%2>0) {
                    mediaList.append(itemel+'</div>');
                    itemel = "<div class=\"onerow\">\n";
                }
            }
            if((items.length-1)%2==0) {
                mediaList.append(itemel+'</div>');
            }
        }
        if($("#mediaTitleHide").is(":checked")){
            currEl.find(".titleBox").addClass("hidden");
        }else{
            currEl.find(".titleBox").removeClass("hidden");
        }
        if($("#mediaMobileHide").is(":checked")){
            currEl.addClass("hidden-xs");
        }else{
            currEl.removeClass("hidden-xs");
        }
    });
    $("#saveAlbum").click(function(e) {
        e.preventDefault();
        currEl.find(".titleBox .title").text($("#albumTitle").val());
        currEl.find(".titleBox .subTitle").text($("#albumSubTitle").val());
        currEl.find("script").remove();
        var albumId = $("#albumContent input[name='albumListRadio']:checked").val();
        var showCount = $("#albumDisplayCount").val();
        var showTitle = $("#albumContent input[name='imgShowTitle']:checked").val();
        currEl.find(".albumList").attr({
            "data-id":albumId,
            "data-count":showCount,
            "data-title":showTitle
        });
        var curID = currEl.find(".albumList").attr("id");
        $("#script-"+curID).remove();
            var scriptStr = '<script id="script-'+curID+'" src="/action/ablumshow.asp?aid='+curID+'&wid='+webID+'&typeid='+albumId+'&topnum='+showCount+'&biaoiti='+showTitle+'"></script>';
        currEl.append(scriptStr);
        if($("#albumTitleHide").is(":checked")){
            currEl.find(".titleBox").addClass("hidden");
        }else{
            currEl.find(".titleBox").removeClass("hidden");
        }
        if($("#albumMobileHide").is(":checked")){
            currEl.addClass("hidden-xs");
        }else{
            currEl.removeClass("hidden-xs");
        }
    });
    $("#saveContact").click(function(e) {
        e.preventDefault();
        currEl.find(".titleBox .title").text($("#contactTitle").val());
        currEl.find(".titleBox .subTitle").text($("#contactSubTitle").val());
        var linkman = $("#contactshowLinkman").is(":checked") ? true : false;
        var mobile = $("#contactShowMobile").is(":checked") ? true : false;
        var abstract = $("#contactShowAbstract").is(":checked") ? true : false;
        var address = $("#contactShowAddress").is(":checked") ? true : false;
        currEl.find(".contactCnt").attr({
            "data-linkman":linkman,
            "data-mobile":mobile,
            "data-abstract":abstract,
            "data-address":address
        });
        if(abstract) {
            currEl.find(".contactCnt .company-desc").removeClass("hidden");
        }else {
            currEl.find(".contactCnt .company-desc").addClass("hidden");
        }
        if(!linkman && !mobile) {
            currEl.find(".contactCnt .contact-type").addClass("hidden");
        }else {
            currEl.find(".contactCnt .contact-type").removeClass("hidden");
            if(linkman) {
                currEl.find(".contactCnt .contact-type .contact-type-content div:eq(0)").removeClass("hidden");
            }else {
                currEl.find(".contactCnt .contact-type .contact-type-content div:eq(0)").addClass("hidden");
            }
            if(mobile) {
                currEl.find(".contactCnt .contact-type .contact-type-content div:eq(1)").removeClass("hidden");
            }else {
                currEl.find(".contactCnt .contact-type .contact-type-content div:eq(1)").addClass("hidden");
            }
        }
        if(address) {
            currEl.find(".contactCnt .contact-address").removeClass("hidden");
        }else {
            currEl.find(".contactCnt .contact-address").addClass("hidden");
        }
        if($("#contactTitleHide").is(":checked")){
            currEl.find(".titleBox").addClass("hidden");
        }else{
            currEl.find(".titleBox").removeClass("hidden");
        }
        if($("#contactMobileHide").is(":checked")){
            currEl.addClass("hidden-xs");
        }else{
            currEl.removeClass("hidden-xs");
        }
    });

    function addPic(dom, imgs) {
        if(dom) {
            if(!imgs || imgs.length==0) {
                imgs = ["img/default_003.jpg"];
            }
            for(var i=0; i<imgs.length; i++) {
                var picIndex = ++multiPicIndex;
                var imgsrc = imgs[i];
                var template= $("<div id=\"m-pic-"+picIndex+"\" class=\"form-group the-item\">\n" +
                    "               <label class=\"col-sm-2 control-label\">* 轮播图"+picIndex+"</label>\n" +
                    "               <div class=\"col-sm-10\">\n" +
                    "                   <div class=\"image-wrapper\">\n" +
                    "                       <img src=\""+imgsrc+"\" width=\"300\" height=\"200\">\n" +
                    "                   </div>\n" +
                    "                   <div class=\"input-wrapper\">\n" +
                    "                       <span class=\"btn btn-info btn-xs\">\n" +
                    "                           <input type=\"file\" name=\"pic\" class=\"btn-uploadpic\" style=\"cursor: pointer;height: 20px;left: 15px;opacity: 0;position: absolute;width: 35px;\">\n" +
                    "                           <i class=\"icon-upload\"></i> 上传\n" +
                    "                       </span>\n" +
                    "                       <button type=\"button\" class=\"btn btn-info btn-xs btn-cutpic\"> 裁剪</button>\n" +
                    "                       <button type=\"button\" class=\"btn btn-info btn-xs btn-delpic\"> 删除</button>\n" +
                    "                   </div>\n" +
                    "                   <input placeholder=\"可选：点击图片跳转地址, http://xxx\" class=\"form-control link-url\"/>\n" +
                    "               </div>\n" +
                    "            </div>");
                $(dom).parent().before(template);
                template.find("input[type='file']").fileupload({
                    url: '/action/upload/',
                    dataType: 'json',
                    add: function (e, data) {
                        var filename = data.files[0].name;
                        var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
                        if(namerex.test(filename)) {
                            data.formData = { fileinpname: 'pic', time: Date() }
                            data.submit();
                        }else {
                            alert('只能上传jpg、png、gif、bmp格式的图片文件');
                        }
                    },
                    done: function (e, data) {
                        if(data.result && data.result.status=='0') {
                            var filename = data.files[0].name;
                            filename = filename.substring(0,filename.length-4);
                            $(this).parent().parent().prev('.image-wrapper').children('img').attr("id","pic-"+filename).attr("src",data.result.relatPath).attr("height","200").attr("width","300");
                        }
                    }
                });
            }
        }
    }

    function addMedia(dom, medias) {
        if(dom) {
            if(!medias || medias.length==0) {
                medias = [{img:"img/default_003.jpg",name:"",desc:"",video:""}];
            }
            for(var i=0; i<medias.length; i++) {
                var mIndex = ++mediaIndex;
                var media = medias[i];
                var template= $("<div id=\"media-"+mediaIndex+"\" class=\"the-item\">\n" +
                    "               <div class=\"form-group\">\n" +
                    "                   <label class=\"col-sm-2 control-label\">* 图文"+mediaIndex+"</label>\n" +
                    "                   <div class=\"col-sm-10\">\n" +
                    "                       <div class=\"image-wrapper\">\n" +
                    "                           <img src=\""+media.img+"\" height=\"200\" width=\"300\">\n" +
                    "                       </div>\n" +
                    "                       <div class=\"input-wrapper\">\n" +
                    "                           <span class=\"btn btn-info btn-xs\">\n" +
                    "                               <input name=\"pic\" class=\"btn-uploadpic\" style=\"cursor: pointer;height: 20px;left: 15px;opacity: 0;position: absolute;width: 35px;\" type=\"file\">\n" +
                    "                               <i class=\"icon-upload\"></i> 上传\n" +
                    "                           </span>\n" +
                    "                           <button type=\"button\" class=\"btn btn-info btn-xs btn-cutpic\"> 裁剪</button>\n" +
                    "                           <button type=\"button\" class=\"btn btn-info btn-xs btn-delpic\"> 删除</button>\n" +
                    "                       </div>\n" +
                    "                   </div>\n" +
                    "               </div>\n" +
                    "               <div class=\"form-group\">\n" +
                    "                   <label class=\"col-sm-2 control-label\">*标题</label>\n" +
                    "                   <div class=\"col-sm-10\">\n" +
                    "                       <input type=\"text\" name=\"name\" class=\"form-control\" value=\""+media.name+"\">\n" +
                    "                   </div>\n" +
                    "               </div>\n" +
                    "               <div class=\"form-group\">\n" +
                    "                   <label class=\"col-sm-2 control-label\">*描述</label>\n" +
                    "                   <div class=\"col-sm-10\">\n" +
                    "                       <textarea name=\"desc\" class=\"form-control\">"+media.desc+"</textarea>\n" +
                    "                   </div>\n" +
                    "               </div>\n" +
                    "               <div class=\"form-group\">\n" +
                    "                   <label class=\"col-sm-2 control-label\">视频链接</label>\n" +
                    "                   <div class=\"col-sm-10\">\n" +
                    "                       <input type=\"text\" name=\"video\" class=\"form-control\"  value=\""+media.video+"\" placeholder=\"请填写优酷视频通用代码\">\n" +
                    "                   </div>\n" +
                    "               </div>\n" +
                    "           </div>");
                $(dom).parent().before(template);
                template.find("input[type='file']").fileupload({
                    url: '/action/upload/',
                    dataType: 'json',
                    add: function (e, data) {
                        var filename = data.files[0].name;
                        var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
                        if(namerex.test(filename)) {
                            data.formData = { fileinpname: 'pic', time: Date() }
                            data.submit();
                        }else {
                            alert('只能上传jpg、png、gif、bmp格式的图片文件');
                        }
                    },
                    done: function (e, data) {
                        if(data.result && data.result.status=='0') {
                            var filename = data.files[0].name;
                            filename = filename.substring(0,filename.length-4);
                            $(this).parent().parent().prev('.image-wrapper').children('img').attr("id","pic-"+filename).attr("src",data.result.relatPath).attr("height","200").attr("width","300");
                        }
                    }
                });
            }
        }
    }
    function adaptScaleRatio(v) {
        if(v) {
            if(v>=3.0&&v<=3.4) {
                return 3.2;
            }else if(v>=1.6&&v<=2.0) {
                return 1.8;
            }else if(v>=7.8&&v<=8.2) {
                return 8;
            }else if(v>=5.8&&v<=6.2) {
                return 6;
            }else if(v>=4.8&&v<=5.2) {
                return 5;
            }else if(v>=2.3&&v<=2.7) {
                return 2.5;
            }else if(v>=6.5&&v<=6.9) {
                return 6.7;
            }else if(v>=3.8&&v<=4.2) {
                return 4;
            }else if(v>=9.8&&v<=10.2) {
                return 10;
            }
        }
        return 3.2;
    }

    //--------------相册文件夹相关代码-------------------
    var currPicEl;
    //添加相册文件夹
    $("#albumFolderAdd").click(function () {
        var input = $(this).siblings("#inputAlbumFolderAdd");
        input.val("");
        input.show().focus();
    });
    //添加相册文件夹失去焦点
    $("#inputAlbumFolderAdd").blur(function () {
        var typeName = $(this).val();
        var thisObj = $(this);
        if(typeName.length == 0){
            thisObj.hide();
            return false;
        }else if(typeName.length > 10){
            alert("文件夹的名称不能超过10个");
            return false;
        }
        $.ajax({
            url:"/action/ablum.asp?act=type_add",
            type:"post",
            dataType:"json",
            data:{
                typenames:typeName
            },
            success:function(data){
                if(data.status == 0){
                    var html = "<div class='leftMainItem' data-id='"+data.typeid+"'>"+
                        "<i class='fa fa-bars icox'></i>"+
                        "<a class='leftMainItemName albumFolderName' data-toggle='modal' data-target='#editAlbumContent'>"+data.typename+"</a>"+
                        "<input type='text' style='display: none;' class='inputLeftMainItemName inputAlbumFolderName' data-id='"+data.typeid+"'>"+
                        "<span class='edit'>"+
                        "<a href='javascript:;' class='leftMainItemNameEdit albumFolderNameEdit'><i class='fa fa-pencil'></i></a>"+
                        "<a href='javascript:;' class='albumFolderDel' data-id='"+data.typeid+"'><i class='fa fa-remove'></i></a>"+
                        "</span>"+
                        "</div>";
                    $("#albumList").append(html);
                    thisObj.hide();
                }else{
                    alert(data.message);
                }
            }
        })
    });
    //编辑相册文件夹
    $(document).on("click",".albumFolderNameEdit",function () {
        var aObj = $(this).parent().siblings(".albumFolderName");
        var inputObj = $(this).parent().siblings(".inputAlbumFolderName");
        var txt = aObj.html();
        aObj.hide();
        inputObj.val(txt);
        inputObj.show().focus();
    });
    //编辑相册文件夹失去焦点
    $(document).on("blur",".inputAlbumFolderName",function(){
        var albumTypeID = $(this).attr("data-id");
        var albumTypeName = $(this).val();
        if(albumTypeName.length > 10){
            alert("文件夹的名称不能超过10个字符");
            return false;
        }
        var ele = $(this);
        $.ajax({
            url:"/action/ablum.asp?act=type_edit",
            type:"post",
            dataType:"json",
            data:{
                typeid:albumTypeID,
                typenames:albumTypeName
            },
            async:false,
            success:function(data){
                if(data.status == 0){
                    ele.hide();
                    var aObj = ele.siblings(".albumFolderName");
                    aObj.html(albumTypeName);
                    aObj.show();
                }else{
                    alert(data.message);
                }
            }
        })
    })
    //删除相册文件夹
    $(document).on("click",".albumFolderDel",function(){
        if(confirm("确认要删除此相册吗？")) {
            var albumTypeID = $(this).attr("data-id");
            var thisObj = $(this);
            $.ajax({
                url:"/action/ablum.asp?act=type_del",
                type:"post",
                dataType:"json",
                data:{
                    typeid:albumTypeID
                },
                success:function(data){
                    if(data.status == 0){
                        thisObj.parents(".leftMainItem").remove();
                    }else{
                        alert("文件夹删除失败");
                    }
                }
            })
        }
    });
    //加载相册文件夹内容
    $(document).on("click","[data-target=#editAlbumContent]",function(e) {
        e.preventDefault();
        var albumTypeName = $(this).html();
        var albumTypeID = $(this).parents(".leftMainItem").attr("data-id");
        $("#albumContentTitle").html(albumTypeName);
        $("#albumContentTitle").attr("data-id",albumTypeID);
        $.ajax({
            url:"/action/ablum.asp?act=xc_list",
            type:"post",
            dataType:"json",
            data:{
                typeid:albumTypeID,
                page:1,
                pagenum:100
            },
            success:function (data) {
                if(data.status==0) {
                    if(data.item && data.count>0) {
                        for(var i=0; i<data.item.length; i++) {
                            var item = data.item[i];
                            var picItem = "<div class=\"album-pic\">\n" +
                                "              <img src=\""+shieldSubUrl(item.photo_url)+"\" data-id=\""+item.photo_id+"\" data-small-path=\""+shieldSubUrl(item.photo_url)+"\" data-big-path=\""+shieldSubUrl(item.photo_url_d)+"\" data-oldfilename=\""+item.photo_name+"\">\n" +
                                "              <div class=\"title\">"+item.photo_name+"</div>\n" +
                                "          </div>"
                            $("#editAlbumContent .albumPictures").append(picItem);
                        }
                    }
                }else {
                    alert("加载相册内容失败，请稍后重试！");
                }
            }
        })
    });

    $('#editAlbumContent').on('hidden.bs.modal', function () {
        $("#editAlbumContent .albumPictures").empty();
        $("#editAlbumContent .albumEditor .tipPanel").removeClass("hidden");
        $("#editAlbumContent .albumEditor .editorPanel").addClass("hidden");
    });
    //注册相册文件夹中图片的选择事件
    $(document).on("click",".albumPictures .album-pic",function(e){
        e.preventDefault();
        if(currPicEl) currPicEl.removeClass("active");
        currPicEl = $(this);
        currPicEl.addClass("active");
        var filename = currPicEl.find(".title").text();
        var photoId = currPicEl.find("img").attr("data-id");
        var smallPath = currPicEl.find("img").attr("data-small-path");
        var bigPath = currPicEl.find("img").attr("data-big-path");
        var oldfilename = currPicEl.find("img").attr("data-oldfilename");
        $("#editAlbumContent .albumEditor .tipPanel").addClass("hidden");
        $("#editAlbumContent .albumEditor .editorPanel").removeClass("hidden");
        var img = $(this).parents(".albumContent").find(".albumEditor .pic-group img");
        var inp = $(this).parents(".albumContent").find(".albumEditor .title-group input");
        img.attr("src",smallPath)
            .attr("data-id", photoId)
            .attr("data-small-path", smallPath)
            .attr("data-big-path", bigPath)
            .attr("data-oldfilename", filename);
        inp.val(filename);
    });
    //相册文件夹图片上传
    $("#albumContentAdd").fileupload({
        url: '/action/upload/up_ablum.asp',
        dataType: 'json',
        add: function (e, data) {
            var albumTypeID = $("#albumContentTitle").attr("data-id");
            $.each(data.files, function (index, file){
                var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
                if(namerex.test(file.name)) {
                    data.formData = { act: 'up',fileinpname: 'pic' , typeid: albumTypeID}
                    data.submit();
                }else {
                    alert('只能上传jpg、png、gif、bmp格式的图片文件');
                }
            });
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.result.oldfilename;
                filename = filename.substring(0,filename.length-4);
                var container = $(this).parents(".modal-content").find(".albumPictures");
                var picItem = "<div class=\"album-pic\">\n" +
                    "              <img src=\""+shieldSubUrl(data.result.smallPath)+"\" data-id=\""+data.result.photo_id+"\" data-small-path=\""+shieldSubUrl(data.result.smallPath)+"\" data-big-path=\""+shieldSubUrl(data.result.bigPath)+"\" data-oldfilename=\""+filename+"\">\n" +
                    "              <div class=\"title\">"+filename+"</div>\n" +
                    "          </div>";
                container.append(picItem);
                var albumTypeID = $("#albumContentTitle").attr("data-id");
                updateAlbumShow(albumTypeID);

            }
        }
    });
    //相册文件夹图片编辑
    $("#albumContentEdit").fileupload({
        url: '/action/upload/up_ablum.asp',
        dataType: 'json',
        add: function (e, data) {
            var photoId = $(this).parent().prev('.image-wrapper').children('img').attr("data-id");
            var filename = data.files[0].name;
            var namerex = /^(.+[.])(jpg|png|gif|bmp)$/i;
            if(namerex.test(filename)) {
                data.formData = { act: 'edit', fileinpname: 'pic' , photo_id: photoId}
                data.submit();
            }else {
                alert('只能上传jpg、png、gif、bmp格式的图片文件');
            }
        },
        done: function (e, data) {
            if(data.result && data.result.status=='0') {
                var filename = data.result.oldfilename;
                filename = filename.substring(0,filename.length-4);
                $(this).parent().prev('.image-wrapper').children('img').attr("src",shieldSubUrl(data.result.smallPath))
                    //.attr("data-id",data.result.photo_id)
                    .attr("data-small-path", shieldSubUrl(data.result.smallPath))
                    .attr("data-big-path", shieldSubUrl(data.result.bigPath));
            }
        }
    });
    //相册文件夹图片保存
    $("#albumContentSave").click(function () {
        var albumTypeID = $("#albumContentTitle").attr("data-id");
        var filename = $(this).parents(".editorPanel").find(".title-group input").val();
        var photoId = $(this).parents(".editorPanel").find(".pic-group img").attr("data-id");
        var smallPath = $(this).parents(".editorPanel").find(".pic-group img").attr("data-small-path");
        var bigPath = $(this).parents(".editorPanel").find(".pic-group img").attr("data-big-path");
        var oldfilename = $(this).parents(".editorPanel").find(".pic-group img").attr("data-oldfilename");
        if(filename != '') {
            $.ajax({
                url:"/action/ablum.asp?act=xc_edit",
                type:"post",
                dataType:"json",
                data:{
                    photo_id: photoId,
                    photo_name:filename
                },
                success:function (data) {
                    if(data.status==0) {
                        currPicEl.find('img').attr("src",smallPath)
                            .attr("data-id", photoId)
                            .attr("data-small-path", smallPath)
                            .attr("data-big-path", bigPath)
                            .attr("data-oldfilename", filename);
                        currPicEl.find('.title').text(filename);
                        updateAlbumShow(albumTypeID);
                    }else {
                        alert("保存失败，请稍后重试！");
                    }
                }
            })
        }else {
            alert("请输入图片标题！");
        }
    });
    //相册文件夹图片删除
    $("#albumContentDel").click(function () {
        var photoId = $(this).parents(".editorPanel").find(".pic-group img").attr("data-id");
        var albumTypeID = $("#albumContentTitle").attr("data-id");
        if(photoId) {
            if(confirm("确认删除此图片？")) {
                $.ajax({
                    url:"/action/ablum.asp?act=xc_del",
                    type:"post",
                    dataType:"json",
                    data:{
                        photo_id: photoId
                    },
                    success:function (data) {
                        if(data.status==0) {
                            currPicEl.remove();
                            $("#albumContentDel").parents(".editorPanel").find(".pic-group img").attr("data-id","").attr("src","");
                            $("#albumContentDel").parents(".editorPanel").find(".title-group input").val("");
                            updateAlbumShow(albumTypeID);
                        }else {
                            alert("删除失败，请稍后重试！");
                        }
                    }
                })
            }
        }else {
            alert("请选择一张图片再删除！");
        }
    });
    //更新相册展示
    function updateAlbumShow(folderID){
        $("script[id*='script-album']").each(function(){
            var src = $(this).attr("src");
            var albumID = getParam(src,"aid");
            if(folderID == getParam(src,"typeid")){
                $("#script-" + albumID).remove();
                $(".demo").append("<script id='script-"+albumID+"' src='"+src+"'></script>");
            }
        })
    }
    function shieldSubUrl(uri) {
        if(uri) {
            if(/^http.*/.test(uri)) {
                return uri;
            }else {
                if(/^\/.*/.test(uri)) {
                    return webUrl + uri;
                }else {
                    return  webUrl + "/" + uri;
                }
            }
        }else {
            return "";
        }
    }

});










